package com.tanhua.server.exception;

import com.tanhua.vo.ErrorResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 全局异常捕捉器
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(BusinessException.class)  //捕捉BusinessException异常
    public ResponseEntity ExceptionHandler(BusinessException exception) {
        //打印错误信息
        exception.printStackTrace();
        //返回错误信息
        ErrorResult errorResult = exception.getErrorResult();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    @ExceptionHandler(Exception.class)  //捕捉未知异常
    public ResponseEntity ExceptionHandler(Exception exception) {
        //打印错误信息
        exception.printStackTrace();
        //返回错误信息
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());
    }
}
